package br.ufpb.di.knowledgetv.sqtv.services;

import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import org.springframework.stereotype.Component;

import br.ufpb.di.knowledgetv.sqtv.utils.SPARQLUtils;

import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;

//@Component
public class LinkedMDBService extends AbstractService {

	
	public List<Resource> searchByKeyword(String keyword) {
        List<Resource> result = new ArrayList<Resource>();
        try {
            String sparql = SPARQLUtils.readFileSparql(getClass().getResource("/sparqls/query_linkedmdb.sparql").getPath(), keyword);
            ResultSet rs = SPARQLUtils.executeQuery("http://data.linkedmdb.org/sparql", sparql);

            while( rs.hasNext() ) {
                QuerySolution qs = rs.nextSolution();
                Resource re = qs.getResource("thing");
                re.addLiteral(RDFS.label, qs.getLiteral("label"));
                re.addProperty(RDFS.domain, this.beanName);
                if (qs.getLiteral("abstract") != null)
                    re.addLiteral(RDFS.comment, qs.getLiteral("abstract"));
                re.addProperty(RDF.type, qs.get("type"));
                result.add(re);
                return result;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
	}

    @Override
    public void setBeanName(String name) {
        this.beanName = name;
    }

    @Override
    public Model getModel(String url) {
        Model model = ModelFactory.createDefaultModel();
        return model.read(url);
    }

}
